﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using System.Web.Security;
using mscook.mvc.Core;
using mscook.mvc.Areas.weblogin.Models;
using System.Linq.Expressions;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    [UserAuthorizeAttribute(Roles = "admin")]
    public class LoginUserOfVipController : BaseController
    {
        //
        // GET: /weblogin/LoginUserOfVip/
        private ILoginUserService iLoginUserService;
        private ILoginUserOfVipRepository iLoginUserOfVipRepository;
        public LoginUserOfVipController(ILoginUserService iLoginUserService, ILoginUserOfVipRepository iLoginUserOfVipRepository)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iLoginUserOfVipRepository = iLoginUserOfVipRepository;
        }
        public ActionResult Index(string nowPageNum, string per = "", string userName = "", bool isPassed = false)
        {
            Expression<Func<LoginUserOfVip, bool>> select_expression = o => (string.IsNullOrEmpty(userName) || o.LoginUser.LoginName.Contains(userName))
               && (o.IsPassed==isPassed);

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<LoginUserOfVip> iLoginUserOfVipList = iLoginUserOfVipRepository.SelectLimitByOrder(pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();

            ViewData["userName"] = userName;
            ViewData["isPassed"] = isPassed;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(iLoginUserOfVipList);
        }

        [HttpGet]
        public ActionResult Edit(string id)
        {
            LoginUserOfVip loginUserOfVip = iLoginUserOfVipRepository.Get(id);
            if (loginUserOfVip != null)
            {
                LoginUserOfVipModel loginUserOfVipModel = new LoginUserOfVipModel() { LoginUserOfVip = loginUserOfVip, LoginUser = CurrentUser };
                loginUserOfVipModel.DateTime = loginUserOfVip.LoginUser.VipInvalidDate;
                InitNavBar();
                return View(loginUserOfVipModel);
            }
            return RedirectToIndex();
        }
        [HttpPost]
        public ActionResult Edit(string id,LoginUserOfVipModel model)
        {
            LoginUserOfVip loginUserOfVip = iLoginUserOfVipRepository.Get(id);
            if (loginUserOfVip != null)
            {
                loginUserOfVip.LoginUser.VipInvalidDate = model.DateTime;
                loginUserOfVip.LoginUser.Bonus += Math.Abs(model.AddBonus);
                loginUserOfVip.IsPassed = true;
                iLoginUserOfVipRepository.SaveOrUpdate(loginUserOfVip);

            }
            return RedirectToIndex();
        }

        public ActionResult Delete(string id)
        {
            iLoginUserOfVipRepository.PhysicsDelete(id);
            return RedirectToIndex();
        }

    }
}
